// stores/domainStore.ts - Z�:��
import { create } from 'zustand';
import { domainService } from '../services/domains';
import type { DomainDetail, DomainSummary, CreateDomainRequest, ApiResponse, DomainStats } from '../types/api';

interface DomainStore {
  // �
  domains: DomainSummary[];
  currentDomain: DomainDetail | null;
  loading: boolean;
  error: string | null;
  domainStats: Record<string, DomainStats>;

  // �\
  fetchDomains: (connection: string) => Promise<void>;
  getDomainDetail: (connection: string, name: string) => Promise<DomainDetail>;
  createDomain: (connection: string, config: CreateDomainRequest) => Promise<ApiResponse>;
  startDomain: (connection: string, name: string) => Promise<ApiResponse>;
  shutdownDomain: (connection: string, name: string) => Promise<ApiResponse>;
  forceStopDomain: (connection: string, name: string) => Promise<ApiResponse>;
  rebootDomain: (connection: string, name: string) => Promise<ApiResponse>;
  suspendDomain: (connection: string, name: string) => Promise<ApiResponse>;
  resumeDomain: (connection: string, name: string) => Promise<ApiResponse>;
  deleteDomain: (connection: string, name: string) => Promise<ApiResponse>;
  getDomainStats: (connection: string, name: string) => Promise<DomainStats>;
  setDomainMemory: (connection: string, name: string, memory: number) => Promise<ApiResponse>;
  setDomainVCPUs: (connection: string, name: string, vcpus: number) => Promise<ApiResponse>;
  clearError: () => void;
}

export const useDomainStore = create<DomainStore>((set, get) => ({
  // ˶
  domains: [],
  currentDomain: null,
  loading: false,
  error: null,
  domainStats: {},

  // ��Z�:h
  fetchDomains: async (connection: string) => {
    set({ loading: true, error: null });
    try {
      const response = await domainService.listDomains(connection);
      set({
        domains: response.domains || response.items || [],
        loading: false
      });
    } catch (error) {
      set({
        error: 'Failed to fetch domains',
        loading: false
      });
    }
  },

  // ��Z�:��
  getDomainDetail: async (connection: string, name: string) => {
    set({ loading: true, error: null });
    try {
      const domain = await domainService.getDomainDetail(connection, name);
      set({
        currentDomain: domain,
        loading: false
      });
      return domain;
    } catch (error) {
      set({
        error: 'Failed to fetch domain details',
        loading: false
      });
      throw error;
    }
  },

  // �Z�:
  createDomain: async (connection: string, config: CreateDomainRequest) => {
    set({ loading: true, error: null });
    try {
      const response = await domainService.createDomain(connection, config);
      
      await get().fetchDomains(connection);
      set({ loading: false });
      return response;
    } catch (error) {
      set({
        error: 'Failed to create domain',
        loading: false
      });
      throw error;
    }
  },

  // /�Z�:
  startDomain: async (connection: string, name: string) => {
    try {
      const response = await domainService.startDomain(connection, name);
      
      await get().fetchDomains(connection);
      return response;
    } catch (error) {
      set({ error: 'Failed to start domain' });
      throw error;
    }
  },

  // s�Z�:
  shutdownDomain: async (connection: string, name: string) => {
    try {
      const response = await domainService.shutdownDomain(connection, name);
      
      await get().fetchDomains(connection);
      return response;
    } catch (error) {
      set({ error: 'Failed to shutdown domain' });
      throw error;
    }
  },

  // :6\bZ�:
  forceStopDomain: async (connection: string, name: string) => {
    try {
      const response = await domainService.forceStopDomain(connection, name);
      
      await get().fetchDomains(connection);
      return response;
    } catch (error) {
      set({ error: 'Failed to force stop domain' });
      throw error;
    }
  },

  // �/Z�:
  rebootDomain: async (connection: string, name: string) => {
    try {
      const response = await domainService.rebootDomain(connection, name);
      return response;
    } catch (error) {
      set({ error: 'Failed to reboot domain' });
      throw error;
    }
  },

  // �\Z�:
  suspendDomain: async (connection: string, name: string) => {
    try {
      const response = await domainService.suspendDomain(connection, name);
      
      await get().fetchDomains(connection);
      return response;
    } catch (error) {
      set({ error: 'Failed to suspend domain' });
      throw error;
    }
  },

  // b
  resumeDomain: async (connection: string, name: string) => {
    try {
      const response = await domainService.resumeDomain(connection, name);
      await get().fetchDomains(connection);
      return response;
    } catch (error) {
      set({ error: 'Failed to resume domain' });
      throw error;
    }
  },

  //  dZ�:
  deleteDomain: async (connection: string, name: string) => {
    set({ loading: true, error: null });
    try {
      const response = await domainService.deleteDomain(connection, name);
      
      await get().fetchDomains(connection);
      set({ loading: false });
      return response;
    } catch (error) {
      set({
        error: 'Failed to delete domain',
        loading: false
      });
      throw error;
    }
  },

  // ��Z�:ߡ�o
  getDomainStats: async (connection: string, name: string) => {
    try {
      const stats = await domainService.getDomainStats(connection, name);
      set(state => ({
        domainStats: {
          ...state.domainStats,
          [`${connection}:${name}`]: stats
        }
      }));
      return stats;
    } catch (error) {
      set({ error: 'Failed to fetch domain stats' });
      throw error;
    }
  },

  // �nZ�:�X
  setDomainMemory: async (connection: string, name: string, memory: number) => {
    try {
      const response = await domainService.setDomainMemory(connection, name, memory);
      // Ͱ��Z�:��
      await get().getDomainDetail(connection, name);
      return response;
    } catch (error) {
      set({ error: 'Failed to set domain memory' });
      throw error;
    }
  },

  // �nZ�: vCPU p�
  setDomainVCPUs: async (connection: string, name: string, vcpus: number) => {
    try {
      const response = await domainService.setDomainVCPUs(connection, name, vcpus);
      // Ͱ��Z�:��
      await get().getDomainDetail(connection, name);
      return response;
    } catch (error) {
      set({ error: 'Failed to set domain vCPUs' });
      throw error;
    }
  },

  // d�
  clearError: () => set({ error: null }),
}));